package com.dau.system.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dau.common.annotation.CurrentUser;
import com.dau.common.page.PageQuery;
import com.dau.common.page.TableDataInfo;
import com.dau.common.result.R;
import com.dau.entity.system.user.SysUser;
import com.dau.service.system.user.SysUserService;
import jakarta.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 *
 * @author tzj
 * @date 2024/11/08
 */
@RestController
@RequestMapping("sys-user")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    /**
     * 用户信息
     *
     * @param id id
     * @return {@link R }<{@link Boolean }>
     */
    @GetMapping("/{id}")
    @RequiresPermissions("user:info:query")
    public R<SysUser> getInfo(@PathVariable Long id) {
        return R.success(sysUserService.selectUserInfo(id));
    }

    /**
     * 分页用户
     *
     * @param pageQuery 分页参数
     * @param sysUser   系统用户dto
     * @return {@link R }<{@link IPage }<{@link SysUser }>>
     */
    @GetMapping("/page")
    @RequiresPermissions("user:info:query")
    public TableDataInfo<SysUser> selectUserPage(SysUser sysUser, PageQuery pageQuery) {
        return (sysUserService.selectUserPage(sysUser, pageQuery));
    }

    /**
     * 添加或更新
     *
     * @param sysUser     系统用户
     * @param currentUser 用户
     * @return {@link R }<{@link Boolean }>
     */
    @PostMapping("/addOrUpdate")
    @RequiresPermissions("user:info:update")
    public R<SysUser> addOrUpdate(@RequestBody @Validated SysUser sysUser, @CurrentUser SysUser currentUser) {
        return R.success(sysUserService.addOrUpdate(sysUser, currentUser.getId()));
    }

    /**
     * 删除
     *
     * @param id id
     * @return {@link R }<{@link Boolean }>
     */
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("user:info:delete")
    public R<Boolean> delete(@PathVariable String id) {
        return R.success(sysUserService.removeById(id));
    }


}
